package com.jbit.examSystem.controller;

import com.github.pagehelper.PageHelper;
import com.jbit.examSystem.param.PageParam;
import com.jbit.examSystem.pojo.GradeInfo;
import com.jbit.examSystem.pojo.Student;
import com.jbit.examSystem.pojo.TableDataInfo;
import com.jbit.examSystem.pojo.User;
import com.jbit.examSystem.service.StudentService;
import com.jbit.examSystem.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;

    @RequestMapping("user")
    public String user(){
        return "system/user/user";
    }

    @RequestMapping("list")
    @ResponseBody
    public TableDataInfo list(Integer userType, String userName, PageParam page){
        TableDataInfo respData = new TableDataInfo();
        List<User>  users = new ArrayList<>();
        if(userType==null){
            userType = 1;
        }
        try {
            PageHelper.startPage(page.getPageNum(), page.getPageSize(), true, true, false);
            if(userType==1){
                users = teacherService.getAllTeacher(userName);
            }else if(userType==2){
                users = studentService.getAllStudent(userName);
            }

            respData.succeed(users, 0);
        } catch (Exception e){
            respData.setCode(500);
            e.printStackTrace();
        }
        return respData;
    }
}
